import 'dart:convert';

import 'package:ac/api/ac_urls.dart';
import 'package:ac/common/ac_chat_util.dart';
import 'package:ac/common/ac_more_util.dart';
import 'package:ac/common/ac_pv_manager.dart';
import 'package:ac/common/ac_translation_util.dart';
import 'package:ac/common/style.dart';
import 'package:ac/data/ac_app_preferences.dart';
import 'package:ac/data/ac_user_manager.dart';
import 'package:ac/page/ac_me/ac_me_logic.dart';
import 'package:ac/page/ac_my_posts/ac_my_posts_logic.dart';
import 'package:ac/route/ac_route_pages.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'ac_post_detail_state.dart';

class AcPostDetailLogic extends GetxController {
  final AcPostDetailState state = AcPostDetailState();
  late final AcMoreUtil moreUtil;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    state.post.value = Get.arguments['post'];
    state.userInfo.value = Get.arguments['userInfo'];
    state.isMyPost = Get.arguments['isMyPost'] ?? false;
    moreUtil = AcMoreUtil(state.userInfo.value!, state.userInfo.refresh,
        blockCallback: () {
      Get.back();
    });
    LogPvManger.stat(page: LogPages.postDetailPage);
  }

  void like() {
    if (state.isMyPost) {
      var old = jsonEncode(state.post.value!.toJson());
      state.post.value!.isLiked = !state.post.value!.isLiked;
      state.post.refresh();
      var newJson = jsonEncode(state.post.value!.toJson());
      AcUserManager.updatePublishPost(old, newJson);
      if (Get.isRegistered<AcMyPostsLogic>()) {
        Get.find<AcMyPostsLogic>().initData();
      }
      return;
    }
    state.post.value!.isLiked = !state.post.value!.isLiked;
    state.post.refresh();
    AcUserManager.updatePostData(state.post.value!);
  }

  void sendComment() {
    if (state.commentController.text.isEmpty) {
      return;
    }
    EasyLoading.show(status: 'sending...', maskType: EasyLoadingMaskType.black);
    Future.delayed(const Duration(milliseconds: 1300), () {
      EasyLoading.dismiss();
      if (state.commentController.text.isNotEmpty) {
        state.post.value!.comment.add(state.commentController.text);
        state.post.refresh();
        AcUserManager.updatePostData(state.post.value!);
        state.commentController.clear();
        FocusScope.of(Get.context!).requestFocus(FocusNode());
      }
    });
  }

  void refreshUserInfo() {
    state.userInfo.value =
        AcUserManager.getUserInfo(state.userInfo.value!.userId!);
    state.userInfo.refresh();
  }

  Future<void> translate() async {
    var record =
        AcAppPreferences.instance.getTranslateRecord(state.post.value!.content);
    if (record != null) {
      state.translationContent.value = record;
      return;
    }
    var translationText =
        await AcTranslationUtil.translation(state.post.value!.content);
    state.translationContent.value = translationText;
    AcAppPreferences.instance
        .setTranslateRecord(state.post.value!.content, translationText);
  }

  void more() {
    moreUtil.show();
  }

  Future<void> chat() async {
    AcChatUtil.openChat(state.userInfo.value!.userId!);
  }

}
